package com.fingard.dsp.bank.directbank.whccb01;

import org.apache.commons.lang.time.DateFormatUtils;

import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.dsp.bank.reqrespobj.statement.TransState;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by F on 2017/7/18.
 */
public class WHCCB01SinglePay extends WHCCB01Base implements ISinglePay {
    /**
     * 同行转账
     *
     * @param p_req1908 请求参数
     * @return 单笔支付响应结果
     */
    protected RetAtsSingle payBetweenSameBanks(ReqSingle p_req1908) throws Exception{
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        String opname = "enterpriseAccPay";
        try {
            busDateTime14 = DateFormatUtils.format(p_req1908.singleDetail.getPayDateTime(), "yyyyMMddHHmmss");
        }catch (ParseException e){
            throw new RuntimeException(e);
        }
        try {
            Map<String, String> params = new HashMap<String, String>();
            params.put("payAccount", p_req1908.singleDetail.payAct);
            params.put("payAccountName", p_req1908.singleDetail.payName);
            params.put("recAccount", p_req1908.singleDetail.recAct);
            params.put("recAccountName", p_req1908.singleDetail.recName);
            params.put("payAmount", p_req1908.singleDetail.payAmount);
            params.put("currencyType", convertCurToBank(p_req1908.singleDetail.payCur));
            params.put("orderFlowNo", generateHeaderPart1(reqHeader));
            params.put("payUse", p_req1908.singleDetail.purpose);
            params.put("trsDateTime", getCommonReqDate(reqHeader));
            String[] tmpStrRet = sendToBank(params, p_req1908.reqHeader, opname);
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = "0";
                    retDetail.respInfo = getErrorMsg(tmpBkReader);
                    retDetail.respCode = "0";
                    retDetail.respInfo = "success";
                    retDetail.reqSeqID = reqHeader.reqSeqID;
                    retDetail.transState = tmpRetCode;
                    retDetail.payInfoCode = retDetail.transState;
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retDetail.respCode = "0";
                retDetail.respInfo = tmpStrRet[1];
                retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                retDetail.transState = "0";
                retDetail.payInfoCode = retDetail.transState;
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.getMessage();
        }
        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    /**
     * 跨行转账
     *
     * @param p_req1908 请求参数
     * @return 单笔支付响应结果
     */
    protected RetAtsSingle payBetweenDifferentBanks(ReqSingle p_req1908) throws Exception{
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        String opname = "otherAccPay";
        try {
            busDateTime14 = DateFormatUtils.format(p_req1908.singleDetail.getPayDateTime(), "yyyyMMddHHmmss");
        }catch (ParseException e){
            throw new RuntimeException(e);
        }
        try {
            Map<String, String> params = new HashMap<String, String>();
            params.put("payAccount", p_req1908.singleDetail.payAct);
            params.put("payAccountName", p_req1908.singleDetail.payName);
            params.put("recAccount", p_req1908.singleDetail.recAct);
            params.put("recAccountName", p_req1908.singleDetail.recName);
            params.put("payAmount", p_req1908.singleDetail.payAmount);
            params.put("currencyType", convertCurToBank(p_req1908.singleDetail.payCur));
            params.put("orderFlowNo", generateHeaderPart1(reqHeader));
            params.put("payUse", p_req1908.singleDetail.purpose);
            params.put("trsDateTime", getCommonReqDate(reqHeader));
            String remRoute;
            if (p_req1908.singleDetail.isLargeAmt() || p_req1908.singleDetail.paySpd.equals("1")) {
                remRoute = "5";
            } else {
                remRoute = "7";
            }
            params.put("remRoute", remRoute);
            params.put("recAccountOpenBank", p_req1908.singleDetail.cnapsCode);
            params.put("recAccountOpenBankName", p_req1908.singleDetail.cnapsName);
            String[] tmpStrRet = sendToBank(params, p_req1908.reqHeader, opname);
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = getRespCode(tmpBkReader);
                    retDetail.respInfo = getErrorMsg(tmpBkReader);
                    retDetail.respCode = "0";
                    retDetail.reqSeqID = reqHeader.reqSeqID;
                    retDetail.transState = tmpRetCode;
                    retDetail.payInfoCode = retDetail.transState;
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
                retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                retDetail.transState = "0";
                retDetail.payInfoCode = retDetail.transState;
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.getMessage();
        }
        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        return p_req1908.singleDetail.sameBnk.equals("1") ? payBetweenSameBanks(p_req1908) : payBetweenDifferentBanks(p_req1908);
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        // 返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail();
        String opname = "otherAccQry";
        try {
            busDateTime14 = DateFormatUtils.format(p_req1944.getBgnDateTime(), "yyyyMMddHHmmss");
        }catch (ParseException e){
            throw new RuntimeException(e);
        }
        try {
            Map<String, String> params = new HashMap<String, String>();
            params.put("orderFlowNo", generateHeaderPart1(p_req1944.reqHeader));
            String[] tmpStrRet = sendToBank(params, p_req1944.reqHeader, opname);
            if (tmpStrRet[0].length() == 0) {
                String returnXml = tmpStrRet[1];
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(returnXml, tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = getRespCode(tmpBkReader);
                    if (success(tmpRetCode)) {
                        tmpRetAtsDetail.respCode = "0";
                        tmpRetAtsDetail.transState = tmpBkReader.getFirstText("/BankData/opRep/RepParam/state");
                        tmpRetAtsDetail.payInfoCode = tmpRetAtsDetail.transState;
                        tmpRetAtsDetail.respInfo = tmpBkReader.getFirstText("/BankData/opRep/RepParam/errorMessage");
                        tmpRetAtsDetail.reqSeqID = p_req1944.reqHeader.reqSeqID;
                    } else {
                        tmpRetAtsDetail.respCode = "-1";
                        tmpRetAtsDetail.transState = TransState.Exception.name();
                        tmpRetAtsDetail.respInfo = getErrorMsg(tmpBkReader);
                        tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
                        tmpRetAtsDetail.payInfoCode = tmpRetAtsDetail.transState;
                    }
                    if (StringHelper.isNullOrEmpty(tmpRetAtsDetail.reqSeqID)) {
                        tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
                    }
                } else {
                    tmpRetAtsDetail.respCode = "-1";
                    tmpRetAtsDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetAtsDetail.respCode = "-1";
                tmpRetAtsDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRetAtsDetail.respCode = "-1";
            tmpRetAtsDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRetDetail(tmpRetAtsDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
